package com.simen.kv;

import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Created by Simen.
 */
public class DbHelper {

    private static final String TAG = "DbHelper";

    private static final String LIST_TABLE = "SELECT name FROM sqlite_master WHERE type='table'";
    private static final String SELECT_ALL = "select * from %1$s";
    private static final String NAME = "name";

    public static SQLiteOpenHelper getSqlOpenHelper(Context context, String name) {
        return getSqlOpenHelper(context, name, 1);
    }

    public static SQLiteOpenHelper getSqlOpenHelper(Context context, String name, int version) {
        return new SQLiteHelper(context, name, version);
    }

    public static void printAllDataBase(Context context){
    }

    public static void printAll(SQLiteDatabase database) {
        database.beginTransaction();
        try {
            List<String> tables = new ArrayList<>();
            Cursor tableCursor = database.rawQuery(LIST_TABLE, null);

            Log.e(TAG, "printDatabase: "+database.toString());
            if (tableCursor != null) {
                Log.e(TAG, DatabaseUtils.dumpCursorToString(tableCursor));

                while (tableCursor.moveToNext()) {
                    int position = tableCursor.getPosition();
                    int columnSize = tableCursor.getColumnCount();

                    Log.e(TAG, "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
                    Log.e(TAG, DatabaseUtils.dumpCurrentRowToString(tableCursor));
                    Log.e(TAG, "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");

                    for (int index = 0; index < columnSize; index++) {
                        String columnName = tableCursor.getColumnName(index);
//                        if (position == 0) {
//                            Log.e(TAG, "columnName: " + columnName);
//                        }
//
//                        printColumnValue(tableCursor, index);

                        if (NAME.equalsIgnoreCase(columnName)) {
                            tables.add(tableCursor.getString(index));
                        }
                    }
                }

                tableCursor.close();
            }

            Iterator<String> iterator = tables.iterator();
            while (iterator.hasNext()) {
                Log.e(TAG, "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
                printTable(database, iterator.next());
                Log.e(TAG, "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
            }

            database.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            database.endTransaction();
            database.close();
        }
    }

    public static void printTable(SQLiteDatabase database, String tableName) {
        Cursor cursor = database.rawQuery(String.format(SELECT_ALL, tableName), null);
        Log.e(TAG, "printTable: " + tableName);
        if (cursor != null) {

            while (cursor.moveToNext()) {
                int position = cursor.getPosition();
                int columnSize = cursor.getColumnCount();

                Log.e(TAG, DatabaseUtils.dumpCurrentRowToString(cursor));
//                for (int index = 0; index < columnSize; index++) {
//                    if (position == 0) {
//                        Log.e(TAG, "columnName: " + cursor.getColumnName(index));
//                    }
//
//                    printColumnValue(cursor, index);
//                }
            }

            cursor.close();
        }
    }

    private static void printColumnValue(Cursor cursor, int index) {
        int type = cursor.getType(index);
        switch (type) {
            case Cursor.FIELD_TYPE_STRING:
                Log.e(TAG, "string : " + cursor.getString(index));
                break;
            case Cursor.FIELD_TYPE_INTEGER:
                Log.e(TAG, "integer : " + cursor.getInt(index));
                break;
            case Cursor.FIELD_TYPE_FLOAT:
                Log.e(TAG, "float : " + cursor.getFloat(index));
                break;
            case Cursor.FIELD_TYPE_BLOB:
                Log.e(TAG, "found blob with " + index);
                break;
            case Cursor.FIELD_TYPE_NULL:
                Log.e(TAG, "found null with " + index);
                break;
            default:
                Log.e(TAG, "unknown type with " + index);
                break;
        }
    }

}
